// import { exportSearchList } from '@/utils/utils'
import { exportSearchList } from '@/utils/listTools'
import { getAllGasInfoList } from '@/api/gasStationManage/gasInfo'

export default {
  data () {
    return {
      
    }
  },
  methods: {
    /**
     *  初始化时间
     * @param {*} modelName 时间
     * @param {Number} range 天数范围（间隔）包含当天(默认)，fe:7天时间间隔，传6就OK
     * @param {*} searchBar 表单对象(用于传参)
     * @param {Boolean}     initTime (modelName, range, searchBar, hasToday = true ,startTime = 'startTime', endTime = 'endTime') {
 是否包含今天
     * @param {String} startTime 开始时间 默认startTime
     * @param {String} endTime 结束时间  默认endTime
     */
    initTime (modelName, range, searchBar, hasToday = true ,startTime = 'startTime', endTime = 'endTime') {
      const timeArr = this.handleTimeRange(range, hasToday)
      const start = this.formatTime(timeArr[0], '00:00:00')
      const end = this.formatTime(timeArr[1], '23:59:59')
      this.$data[modelName] = [start,end]
      this.$data[searchBar][startTime] = start
      this.$data[searchBar][endTime] = end
    },
    /**
     *  时间change
     * @param {Array} value 选择的值
     * @param {String} name searchBar(查询表单对象)
     * @param {String} startTime 开始时间 默认startTime
     * @param {String} endTime 结束时间  默认endTime
     */
    changeTime (value, name, startTime = 'startTime', endTime = 'endTime') {
      let searchObj = this.$data[name]
      if (value) {
        searchObj[startTime] = value[0]
        searchObj[endTime] = value[1]
      } else {
        searchObj[startTime] = ''
        searchObj[endTime] = ''
      }
    },
    //计算时间间隔，range为间隔天数-1， 如果是往前倒时间，传递正数，往后延长时间，传递负数
    handleTimeRange (range, hasToday) {
      let date = new Date()
      let val1 = ''
      let val2 = ''
      const today = hasToday ? date : new Date(date.getTime() - 1 * 24 * 3600 * 1000)
      if(range > 0 ) {
        val1 = new Date(date.getTime() - range * 24 * 3600 * 1000)
        val2 = today
      } else {
        val1 = today
        val2 = new Date(date.getTime() + Math.abs(range) * 24 * 3600 * 1000) 
      }
      let timeArr = [val1, val2]
      return timeArr
    },
    //日期月份单数前边补0,生成时间格式 yyyy-MM-dd HH:mm:ss
    formatTime (val, time) {
      const year = val.getFullYear()
      const month = val.getMonth() + 1
      const day = val.getDate()
      return `${year}-${month > 9 ? month : '0' + month}-${day > 9 ? day : '0' + day} ${time}`
    },
    /**
     *  导出
     * @param {Function} dowanloadApi 
     * @param {String} name 导出文件名称 
     */
    async downloadFile (dowanloadApi,name,otherParams = null) {
      try{
        let data = otherParams ? {...otherParams} : {...this.downloadParams}
        const { code, result = {} } = await dowanloadApi(data)
        if(code !== 200) return
        exportSearchList(result.file, result.fileName)
      }catch(err){
        console.log(err)
      }
    },
    //获取油站
    async getAllGasStations () {
      try{
        const { code, result = [] } = await getAllGasInfoList()
        if(code !== 200) return
        const initObj = { gasId: result }
        this.setSearchConfig(initObj)
      }catch(e){
        console.log(e)
      }
    },
    //包装后台返回的枚举
    optionsWrapper(res){
      let options = Object.entries(res).map(itemArr => {
        return {
          value: itemArr[0],
          label: itemArr[1]
        }
      })
      return options
    }
  }
}
